using System;
using System.ComponentModel;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons.DataFilters;
using AB.ServiceDirectory.Entities;
using Vladsm.Web.UI.WebControls;

namespace AB.ServiceDirectory.Design.WebUserControls.Lookup
{
    /// <summary>
    /// 
    /// </summary>
    public partial class ContactLookupUserControl : UserControl
    {
        /// <summary>
        /// Gets or sets the text box result id.
        /// </summary>
        /// <value>
        /// The text box result id.
        /// </value>
        public string TextBoxResultId
        {
            get
            {
                if (ViewState["TextBoxResultId"] != null)
                {
                    return (string)ViewState["TextBoxResultId"];
                }
                return string.Empty;
            }
            set { ViewState["TextBoxResultId"] = value; }
        }

        /// <summary>
        /// Gets or sets the hidden field result id.
        /// </summary>
        /// <value>
        /// The hidden field result id.
        /// </value>
        public string HiddenFieldResultId
        {
            get
            {
                if (ViewState["HiddenFieldResultId"] != null)
                {
                    return (string)ViewState["HiddenFieldResultId"];
                }

                return string.Empty;
            }
            set { ViewState["HiddenFieldResultId"] = value; }
        }

        /// <summary>
        /// Gets or sets the maximum rows.
        /// </summary>
        /// <value>
        /// The maximum rows.
        /// </value>
        [DefaultValue(15)]
        public int MaximumRows
        {
            get
            {
                if (ViewState["MaximumRows"] != null)
                {
                    return (int)ViewState["MaximumRows"];
                }

                return 15;
            }
            set { ViewState["MaximumRows"] = value; }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var filter = new AContactFilter();
                ViewState["filter"] = filter;
            }
        }

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
        protected override void OnPreRender(EventArgs e)
        {
            GroupRadioButton contact = null;
            if (ContactGridView.Rows.Count > 0)
            {
                contact = ContactGridView.Rows[0].FindControl("RadioChooseContact") as GroupRadioButton;
            }

            if (contact != null)
            {
                contact.Checked = true;
            }
            base.OnPreRender(e);
        }

        /// <summary>
        /// Popups this instance.
        /// </summary>
        public void Popup()
        {
            PanelContainer.CssClass = "popup_container";
            PanelContainer.Visible = true;
            ContactGridView.PageSize = MaximumRows;
        }

        /// <summary>
        /// Handles the Click event of the ButtonSearchContact control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSearchContact_Click(object sender, EventArgs e)
        {
            string firstName = TextBoxFirstNameSearch.Text;
            string Surname = TextBoxSurnameSearch.Text;

            var filter = ViewState["filter"] as AContactFilter;
            if (filter == null)
            {
                return;
            }

            filter.Surname = Surname;
            filter.FirstName = firstName;

            ContactGridView.DataBind();
        }

        /// <summary>
        /// Handles the Click event of the ButtonClose control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonClose_Click(object sender, EventArgs e)
        {
            PanelContainer.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the ButtonSelect control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSelect_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow contactRow in ContactGridView.Rows)
            {
                if (contactRow.RowType == DataControlRowType.DataRow)
                {
                    var grd = (GroupRadioButton)contactRow.FindControl("RadioChooseContact");
                    if (grd != null && grd.Checked)
                    {
                        DataKey dataKey = ContactGridView.DataKeys[contactRow.RowIndex];
                        if (dataKey != null)
                        {
                            if (contactRow.RowIndex < 0)
                            {
                                return;
                            }

                            var key = (int)dataKey.Value;
                            Contact contact = AContactBO.FindContactById(key);

                            Control textBoxContactName = Parent.FindControl(TextBoxResultId);
                            Control hiddenFieldManagerId = Parent.FindControl(HiddenFieldResultId);

                            if (textBoxContactName != null)
                            {
                                (textBoxContactName as TextBox).Text = contact.FirstName + " " + contact.Surname;
                            }

                            if (hiddenFieldManagerId != null)
                            {
                                (hiddenFieldManagerId as HiddenField).Value = key.ToString(CultureInfo.CurrentCulture);
                            }
                        }
                    }
                }
            }
            PanelContainer.Visible = false;
        }

        /// <summary>
        /// Handles the Selecting event of the ContactObjectDataSource control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs"/> instance containing the event data.</param>
        protected void ContactObjectDataSource_Selecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as AContactFilter;
            if (filter != null)
            {
                e.InputParameters["filter"] = filter;
            }
        }

        /// <summary>
        /// Handles the Click event of the ButtonNone control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonNone_Click(object sender, EventArgs e)
        {
            Control textBoxContactName = Parent.FindControl(TextBoxResultId);
            Control hiddenFieldManagerID = Parent.FindControl(HiddenFieldResultId);

            if (textBoxContactName != null)
            {
                (textBoxContactName as TextBox).Text = string.Empty;
            }

            if (hiddenFieldManagerID != null)
            {
                (hiddenFieldManagerID as HiddenField).Value = string.Empty;
            }

            PanelContainer.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the ButtonClear control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonClear_Click(object sender, EventArgs e)
        {
            TextBoxFirstNameSearch.Text = string.Empty;
            TextBoxSurnameSearch.Text = string.Empty;
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBoxInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void CheckBoxInactive_CheckedChanged(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as AContactFilter;
            if (filter != null)
            {
                filter.IncludeInactive = CheckBoxInactive.Checked;
            }

            ContactGridView.DataBind();
        }

        /// <summary>
        /// Handles the Click event of the ButtonEdit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonEdit_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow contactRow in ContactGridView.Rows)
            {
                if (contactRow.RowType == DataControlRowType.DataRow)
                {
                    var grd = (GroupRadioButton)contactRow.FindControl("RadioChooseContact");
                    if (grd != null && grd.Checked)
                    {
                        DataKey dataKey = ContactGridView.DataKeys[contactRow.RowIndex];
                        if (dataKey != null)
                        {
                            if (contactRow.RowIndex < 0)
                            {
                                return;
                            }

                            var key = (int)dataKey.Value;
                            Response.Redirect("~/Contact_HungND12/EditContact.aspx?contactId=" + key);
                        }
                    }
                }
            }
        }
    }
}